package com.qxmly.fm.presenters;

import androidx.annotation.Nullable;

import com.qxmly.fm.interfaces.IRecommendPresenter;
import com.qxmly.fm.interfaces.IrecommendViewCallBack;
import com.qxmly.fm.utils.Constants;
import com.qxmly.fm.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Q
 * Date:2021/11/28$
 * Recommend实现类
 **/
public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";
    private List<IrecommendViewCallBack> mcallbacks =new ArrayList<>();

    private RecommendPresenter(){

    }
    private static RecommendPresenter sInstance = null;
    //获取单例对象
    public static RecommendPresenter getInstance(){
        if(sInstance == null){
            synchronized (RecommendPresenter.class){
                if(sInstance == null){
                    sInstance = new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }

    @Override
    public void getRecommend() {
        updateLoading();
        //获取推荐内容
            //封装参数
            Map<String, String> map = new HashMap<String, String>();
            //一页数据返回条数
            map.put(DTransferConstants.LIKE_COUNT, Constants.RECOMMEND_COUNT+"");
            CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {


                @Override
                //获取成功
                public void onSuccess(@Nullable GussLikeAlbumList gussLikeAlbumList) {

                    LogUtil.d(TAG,"thread name ->"+Thread.currentThread().getName());
                    if(gussLikeAlbumList!=null){
                        List<Album> album_list=gussLikeAlbumList.getAlbumList();
                        // LogUtil.d(TAG,"size->"+album_list);
                        //upRecommendUI(album_list);
                        handleRecommendResult(album_list);
                    }
                }

            @Override
            //获取失败
            public void onError(int i, String s) {
                LogUtil.d(TAG,"error ->"+i);
                LogUtil.d(TAG,"errorMsg->"+s);
                handlerError();

            }
            });
    }

    private void handlerError() {
        if (mcallbacks != null) {
            for (IrecommendViewCallBack callBack : mcallbacks) {
                callBack.onNetWorkError();
            }
        }
    }

    private void handleRecommendResult(List<Album> albumList) {
        //通知UI更新
        if (albumList != null) {
            //albumList.clear();
            if (albumList.size() == 0) {
                for(IrecommendViewCallBack callBack : mcallbacks){
                    callBack.onEmpty();
                }

            } else {
                for (IrecommendViewCallBack callBack : mcallbacks) {
                    callBack.onRecommendLoad(albumList);
                }
            }
        }

    }
    private void updateLoading(){
        for(IrecommendViewCallBack callBack : mcallbacks){
            callBack.onLoading();
        }
    }
//    @Override
//    public void pullRefresh() {
//
//    }
//
//    @Override
//    public void logdMore() {
//
//    }

    @Override
    public void registerViewCallBack(IrecommendViewCallBack callBack) {
      if(mcallbacks!=null&&!mcallbacks.contains(callBack)){
            mcallbacks.add(callBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(IrecommendViewCallBack callBack) {
        if(mcallbacks!=null){
            mcallbacks.remove(callBack);
        }

    }

}
